package com.system.sysUser.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.sysUser.pojo.SysUser;
import com.system.sysUser.mapper.SysUserMapper;
import com.system.sysUser.service.SysUserService;
import com.utils.common.ResponseEnum;
import com.utils.common.ServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* 描述:
* author: srdl
* date: 2022-01-03
*/

@Service
public class SysUserServiceImpl implements SysUserService {

    private static Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper ;


    @Override
    public ServerResponse list() {
        try {
            List<SysUser> jsonObject = sysUserMapper.list();
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse pageList(PageInfo pageInfo) {
        Map<String,Object> returnMap=new HashMap<>();
        try {
            PageHelper.startPage(pageInfo);
            List<SysUser> list = sysUserMapper.list();
            PageInfo<SysUser> jsonObject=new PageInfo<SysUser>(list);
            returnMap.put("list",jsonObject.getList());
            returnMap.put("pages",jsonObject.getPages());
            returnMap.put("total",jsonObject.getTotal());
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(returnMap);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse get(String id) {
        try {
            SysUser jsonObject = sysUserMapper.get(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.GET_SUCCESS).data(jsonObject);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).data(e);
        }
    }

    @Override
    public ServerResponse addSysUser(SysUser sysUser) {
        try {
            sysUser.setId(UUID.randomUUID().toString());
            sysUserMapper.addSysUser(sysUser);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.ADD_SUCCESS).data(sysUser);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("添加失败");
        }
    }

    @Override
    public ServerResponse deleteSysUser(String id) {
        try {
            sysUserMapper.deleteSysUser(id);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.DELETE_SUCCESS);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("删除失败");
        }
    }
    @Override
    public ServerResponse updateSysUser(SysUser sysUser) {
        try{
            sysUserMapper.updateSysUser(sysUser);
            return ServerResponse.getInstance().responseEnum(ResponseEnum.UPDATE_SUCCESS).data(sysUser);
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR).message("修改失败");
        }
    }

    @Override
    public ServerResponse login(SysUser sysUser) {
        try{
            String id = sysUserMapper.login(sysUser);
            if (!StringUtils.isEmpty(id)){
                sysUser.setId(id);
                return ServerResponse.getInstance().responseEnum(ResponseEnum.LOGIN_SUCCESS).data(sysUser);
            }else{
                return ServerResponse.getInstance().responseEnum(ResponseEnum.LOGIN_FAILED);
            }
        }catch (Exception e){
            return ServerResponse.getInstance().responseEnum(ResponseEnum.INNER_ERROR);
        }
    }
}
